package com.bensoft.minesweeper.gui.minesweeperframe.displaypanel;

import com.bensoft.minesweeper.ApplicationController;
import com.bensoft.minesweeper.resources.FileResources;
import com.bensoft.minesweeper.resources.FileResources.ThemeChangeListener;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

/**
 *
 * @author Ben Wolsieffer
 */
public class SmileyButton extends JPanel {

    private Image image, oldImage;
    private boolean inBounds = true;
    private State state = State.SMILE;
    private BevelBorder raisedBorder;
    private BevelBorder loweredBorder;
    private boolean pressed = false;

    private enum State {

        SMILE,
        SUPRISED,
        SUN,
        DEAD
    }

    public SmileyButton() {
        setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        setPreferredSize(new Dimension(22, 22));
        setMaximumSize(getPreferredSize());
        FileResources.addThemeChangeListener(new ThemeChangeListener() {
            @Override
            public void themeChanged() {
                switch (state) {
                    case SMILE:
                        image = FileResources.getSmileImage();
                        break;
                    case SUN:
                        image = FileResources.getSunSmileImage();
                        break;
                    case SUPRISED:
                        image = FileResources.getOhSmileImage();
                        break;
                    case DEAD:
                        image = FileResources.getDeadSmileImage();
                }
                Color outerShadow = FileResources.getColor("smileybutton.border.outershadow");
                Color innerShadow = FileResources.getColor("smileybutton.border.innershadow");
                Color outerHighlight = FileResources.getColor("smileybutton.border.outerhighlight");
                Color innerHighlight = FileResources.getColor("smileybutton.border.innerhighlight");
                raisedBorder = new BevelBorder(BevelBorder.RAISED, outerHighlight, innerHighlight, outerShadow, innerShadow);
                loweredBorder = new BevelBorder(BevelBorder.LOWERED, outerHighlight, innerHighlight, outerShadow, innerShadow);
                setBackground(FileResources.getColor("smileybutton.color"));
                repaint();
                if (pressed && inBounds) {
                    setBorder(loweredBorder);
                } else {
                    setBorder(raisedBorder);
                }
            }
        });
        MouseAdapter ma = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                oldImage = image;
                image = FileResources.getClickedSmileImage();
                setBorder(loweredBorder);
                inBounds = true;
                pressed = true;
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                oldImage = null;
                image = FileResources.getSmileImage();
                setBorder(raisedBorder);
                if (inBounds) {
                    ApplicationController.getGameManager().resetGame();
                }
                pressed = false;
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                if(!contains(e.getPoint())){
                    image = oldImage;
                    setBorder(raisedBorder);
                    inBounds = false;
                } else if (oldImage != null) {
                    image = FileResources.getClickedSmileImage();
                    setBorder(loweredBorder);
                    inBounds = true;
                }
            }
        };
        addMouseListener(ma);
        addMouseMotionListener(ma);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //g.drawImage(image, (getWidth() / 2) - 10, 0, this);
        g.drawImage(image, 0, 0, this);
    }

//    @Override
//    protected void paintBorder(Graphics g) {
//        Border border = getBorder();
//        if (border != null) {
//            border.paintBorder(this, g, (getWidth() / 2) - 10, 0, 22, getHeight());
//        }
//    }
    public void kill() {
        image = FileResources.getDeadSmileImage();
        state = State.DEAD;
        repaint();
    }

    public void win() {
        image = FileResources.getSunSmileImage();
        state = State.SUN;
        repaint();
    }

    public void surprise() {
        image = FileResources.getOhSmileImage();
        state = State.SUPRISED;
        repaint();
    }

    public void unSurprise() {
        image = FileResources.getSmileImage();
        state = State.SMILE;
        repaint();
    }
}
